<?php

class goods extends bhCommon {



    /**
     * 商品分类
     */
    public function category()
    {
        $category = p_getall('category', array('status' => 1), array(), '', 'sort desc');

        $sql = "SELECT A.id,A.price,A.bargain,A.time_end,B.cover_image,B.name FROM " . prefix('order') . " AS A INNER JOIN " . prefix('goods') . " AS B ON A.goods_id=B.id WHERE A.member_id={$this->member['id']} AND A.status=1 AND A.time_end>" . time() . ' ORDER BY A.id DESC';

        $bargain_goods = p_fetchall($sql);
        if ($bargain_goods) {
            foreach ($bargain_goods as & $value) {
                $value['cover_image'] = getImage($value['cover_image']);
                $value['surplus_time'] = $value['time_end'] - time();
                $value['surplus_money'] = round($value['price'] - $value['bargain']);
                $value['d'] = $value['h'] = $value['m'] = $value['s'] = '00';
            }
        }

        $images = array(
            'bargain' => $this->images('bargain_img', 'afWNrt.png')
        );
        $config = array(
            'bargain_style' => getConfig('bargain_style', '#fa0034'),
            'screen_id' => getConfig('screen_id'),
            'goods_list_bargain_ad' => getConfig('goods_list_bargain_ad'),
        );

        $data = array(
            'category' => $category,
            'images' => $images,
            'config' => $config,
            'bargain_goods' => $bargain_goods,
            'is_auth' => $this->is_auth,
        );
        $this->json($data);
    }


    /**
     * 分类下商品详情
     */
    public function goods_list()
    {
        $category_id = intval($this->get('category_id'));

        $goods = p_getall('goods', array('category_id' => $category_id, 'is_delete' => 1, 'status' => 1), array(), '', 'sort desc,id desc');

        if ($goods) {
            foreach ($goods as & $value) {
                $value['cover_image'] = getImage($value['cover_image']);
                $value['succ_count'] = p_getcolumn('order', array('goods_id' => $value['id'], 'status' => 2, 'type' => 1), 'count(*)') ? : 0;
            }
        }


        $data = array(
            'goods' => $goods
        );
        $this->json($data);
    }


    /**
     * 商品详情
     */
    public function details()
    {
        $id = intval($this->get('id'));

        $details = p_get('goods', array('id' => $id, 'is_delete' => 1));
        if (empty($details)) {
            $this->json('商品不存在', 0);
        }

        $details['images'] = json_decode($details['images'], true);
        $details['images'] = array_map('getImage', $details['images']);

        $bargain = array(
            'number' => p_getcolumn('order', array('goods_id' => $id, 'status' => 1), 'count(*)') ? : 0,
            'order' => array()
        );
        if ($bargain['number'] > 0) {
            $limit = $bargain['number'] > 9 ?  9 : $bargain['number'];

            $sql = "SELECT B.headimgurl FROM " . prefix('order') . " AS A INNER JOIN " . prefix('member') . " AS B ON A.member_id=B.id WHERE A.goods_id={$id} AND A.status=1 ORDER BY suc_time desc LIMIT {$limit}";

            $bargain['order'] = p_fetchall($sql);
        }

        $sql = "SELECT id FROM " . prefix('order') . " WHERE member_id={$this->member['id']} AND goods_id={$id} AND time_end >" . time();
        $my_bargain = p_fetch($sql);

        $config = array(
            'integral_name' => getConfig('integral_name', '积分'),
            'goods_bargain_ad' => getConfig('goods_bargain_ad'),
            'special_reminder' => getConfig('special_reminder', '下单后6个工作日内发货, 新疆、西藏、内蒙古、港澳台地区暂不支持'),
        );

        $data = array(
            'details' => $details,
            'bargain' => $bargain,
            'config' => $config,
            'my_bargain' => $my_bargain,
            'is_auth' => $this->is_auth
        );
        $this->json($data);
    }


    /**
     * 兑换记录
     */
    public function log()
    {
        $p = $this->get('p');
        $id = intval($this->get('id'));

        $page_size = 10;
        $limit = ($p - 1) * $page_size;

        $sql = "SELECT B.headimgurl,A.suc_time FROM " . prefix('order') . " AS A INNER JOIN " . prefix('member') . " AS B ON A.member_id=B.id WHERE A.goods_id={$id} AND A.status=1 ORDER BY suc_time desc LIMIT {$limit}, {$page_size}";

        $order = p_fetchall($sql);

        if ($order) {
            foreach ($order as & $value) {
                $value['suc_time'] = formatTime($value['suc_time']);
            }
        }

        $data = array(
            'order' => $order
        );
        $this->json($data);
    }





}